package com.spring.server;

import com.spring.http.Request;
import com.spring.http.Servlet;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Spring King
 * @date 2021/7/11
 */
public class KingService {

    // 本来应该在connector上，这里只有一个，简化直接放在service
    private int port = 8080;

    private List<KingHost> hosts = new ArrayList<>();


    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public List<KingHost> getHosts() {
        return hosts;
    }

    public void setHosts(List<KingHost> hosts) {
        this.hosts = hosts;
    }

    public void addHost(KingHost host) {
        getHosts().add(host);
    }

    public Servlet getServlet(Request request) {
        String url = request.getUrl();
        return getHosts().stream().filter(kingHost -> kingHost.getServlet(url) != null)
                .map(kingHost -> kingHost.getServlet(url))
                .findFirst().orElse(null);
    }
}
